﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Globalization;
using System.Linq;
using EstateNuke.Data.Properties;

namespace EstateNuke.Data
{
    public partial class LeaseContractReceipt
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        public bool Received
        {
            get
            {
                return ReceivableAmount == ReceivedAmount;
            }
        }

        /// <summary>
        /// </summary>
        public decimal RemainingAmount
        {
            get
            {
                return ReceivableAmount - ReceivedAmount;
            }
        }

        /// <summary>
        /// </summary>
        public LeaseContractReceiptScheduledAlert LatestUnProcessedLeaseContractReceiptScheduledAlert
        {
            get
            {
                return InternalLeaseContractReceiptScheduledAlerts.FirstOrDefault(smallOrderExpiredAlert => smallOrderExpiredAlert.IsDeleted == false && smallOrderExpiredAlert.Processed == false);
            }
        }

        /// <summary>
        /// </summary>
        public IList<LeaseContractReceiptScheduledAlert> LeaseContractReceiptScheduledAlerts
        {
            get
            {
                return InternalLeaseContractReceiptScheduledAlerts.Where(smallOrderExpiredAlert => smallOrderExpiredAlert.IsDeleted == false).ToList();
            }
        }

        #endregion

        #region Set Received

        /// <summary>
        /// </summary>
        /// <param name = "modifiedUser"></param>
        public void SetReceived(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            ReceivedDate = DateTime.Now;
            ReceivedAmount = ReceivableAmount;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        /// <summary>
        /// </summary>
        /// <param name = "receivedDate"></param>
        /// <param name = "modifiedUser"></param>
        public void SetReceived(DateTime receivedDate, User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            ReceivedDate = receivedDate;
            ReceivedAmount = ReceivableAmount;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        /// <summary>
        /// </summary>
        /// <param name = "modifiedUser"></param>
        public void SetUnreceived(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            ReceivedDate = null;
            ReceivedAmount = 0;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        /// <summary>
        /// </summary>
        /// <param name = "amount"></param>
        /// <param name = "modifiedUser"></param>
        public void Receive(decimal amount, User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            ReceivedDate = DateTime.Now;
            ReceivedAmount = amount;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        /// <summary>
        /// </summary>
        /// <param name = "receivedDate"></param>
        /// <param name = "receivedAmount"></param>
        /// <param name = "modifiedUser"></param>
        public void Receive(DateTime receivedDate, decimal receivedAmount, User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            ReceivedDate = receivedDate;
            ReceivedAmount = receivedAmount;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion

        #region Create & Remove LeaseContractReceiptScheduledAlert

        /// <summary>
        /// </summary>
        /// <param name = "alertType"></param>
        /// <param name = "modifiedUser"></param>
        public LeaseContractReceiptScheduledAlert CreateLeaseContractReceiptScheduledAlert(AlertType alertType, User modifiedUser)
        {
            if (alertType == null)
            {
                throw new ArgumentNullException("alertType");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (alertType.Id != 110)
            {
                throw new ArgumentException("alertType");
            }
            if (!LeaseContractReference.IsLoaded)
            {
                LeaseContractReference.Load(MergeOption.PreserveChanges);
            }
            if (!LeaseContract.LeaseAgreementReference.IsLoaded)
            {
                LeaseContract.LeaseAgreementReference.Load(MergeOption.PreserveChanges);
            }
            var reminderDays = alertType.DefaultReminderDays.HasValue ? alertType.DefaultReminderDays : 0;
            var alertTypeReminderDays = LeaseContract.LeaseAgreement.Project.AlertTypeReminderDays;
            if (alertTypeReminderDays != null && alertTypeReminderDays.ContainsKey(alertType) && alertTypeReminderDays[alertType] != null && alertTypeReminderDays[alertType].Item1.HasValue)
            {
                reminderDays = alertTypeReminderDays[alertType].Item1;
            }
            if (reminderDays != 0)
            {
                var leaseContractReceiptScheduledAlert = InternalLeaseContractReceiptScheduledAlerts.FirstOrDefault(item => item.IsDeleted == false && item.Processed == false);
                if (leaseContractReceiptScheduledAlert == null)
                {
                    leaseContractReceiptScheduledAlert = new LeaseContractReceiptScheduledAlert
                                                             {
                                                                 RowId = Guid.NewGuid(),
                                                                 CreatedDate = DateTime.Now,
                                                             };
                    InternalLeaseContractReceiptScheduledAlerts.Add(leaseContractReceiptScheduledAlert);
                }
                leaseContractReceiptScheduledAlert.IsDeleted = false;
                leaseContractReceiptScheduledAlert.ModifiedDate = DateTime.Now;
                leaseContractReceiptScheduledAlert.ModifiedByUser = modifiedUser;
                leaseContractReceiptScheduledAlert.AlertType = alertType;
                leaseContractReceiptScheduledAlert.RemindDate = ScheduledDate.AddDays((double) reminderDays);
                return leaseContractReceiptScheduledAlert;
            }
            return null;
        }

        /// <summary>
        /// </summary>
        /// <param name = "leaseContractReceiptScheduledAlert"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveLeaseContractReceiptScheduledAlert(LeaseContractReceiptScheduledAlert leaseContractReceiptScheduledAlert, User modifiedUser)
        {
            if (leaseContractReceiptScheduledAlert == null)
            {
                throw new ArgumentNullException("leaseContractReceiptScheduledAlert");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalLeaseContractReceiptScheduledAlerts.Contains(leaseContractReceiptScheduledAlert))
            {
                leaseContractReceiptScheduledAlert.Delete(modifiedUser);
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            if (ReceivableAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LeaseContractReceipt", "ReceivableAmount"));
            }
            if (ReceivedAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LeaseContractReceipt", "ReceivedAmount"));
            }
            Comment = string.IsNullOrEmpty(Comment) ? null : Comment.Length > 512 ? Comment.Substring(0, 512) : Comment;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<LeaseContractReceiptScheduledAlert>) LeaseContractReceiptScheduledAlerts).ForEach(leaseContractReceiptScheduledAlert => leaseContractReceiptScheduledAlert.Delete(modifiedUser));
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}